package pl.gettoknowme.mobile.android.services.sms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

/**
 * Odbieracz SMS-ów w celu ich analizy
 * @author Michał Kołodziejski
 */
public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras == null)
            return;

        Object[] pdus = (Object[]) extras.get("pdus");

        for (int i = 0; i < pdus.length; i++) {
            SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
            String fromAddress = message.getOriginatingAddress();
            String smsMessageBody = message.getMessageBody().toString();

            
            Toast toast = Toast.makeText(context, "From: "+fromAddress+", BODY: " + smsMessageBody, 
                    Toast.LENGTH_LONG);
            toast.show();
        }

    }

}
